package it.bit.util;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 * Class to handle properties on file
 * @author PerezDeQueya80
 */
public class PropertiesFile extends Properties {

	private static final long serialVersionUID = -5258485346036736741L;

	public File file = null;

	/** Creates a new instance of PropertiesFile */
	public PropertiesFile() {
	}

	/**
	 * Constructor to link properties to a file
	 * @param p_file
	 * @throws IOException
	 */
	public PropertiesFile(File p_file) throws IOException {
		if (p_file!=null) {
			file = p_file;
			if (file.exists()) {
				this.load(file.toURL());
			}
		}
	}

	/**
	 * Save current properties
	 * @throws IOException
	 */
	public void save() throws IOException {
		try {
			java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
			this.store(fos, null);
		}
		catch (java.io.FileNotFoundException e1) {
			e1.printStackTrace();
			System.out.println("What's the problem !?!?");
		}
		catch (IOException e2) {
			throw e2;
		}
	}

	/**
	 * Load properties from a URL
	 * @param url
	 * @throws IOException
	 */
	protected void load(java.net.URL url) throws IOException {
		java.io.InputStream is = url.openStream();
		this.load(is);
		is.close();
	}

}
